﻿using Sundry.IDAL;
using Sundry.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sundry.DAL
{
    public class AdminOperationRepository : BaseRepository<AdminOperation>, IAdminOperationRepository
    {
        /// <summary>
        /// 获取操作日志列表
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="authoryId">权限Id</param>
        /// <param name="nowNumber">当夜数字</param>
        /// <param name="pageSize">页长</param>
        /// <param name="rowCount">总数目</param>
        /// <returns>获取操作日志列表</returns>
        public IQueryable<dynamic> LoadOperstionList(string username, int authoryId, int nowNumber, int pageSize, out int rowCount)
        {
            var result = from a in db.Set<AdminOperation>()
                         join b in db.Set<Admin>() on a.AdminId equals b.AdminId into b_join
                         from b in b_join.DefaultIfEmpty()
                         join c in db.Set<Authory>() on a.AuthoryId equals c.AuthoryId into c_join
                         from c in c_join.DefaultIfEmpty()
                         orderby
                           a.CreateTime descending
                         select new
                         {
                             a.Action,
                             a.Content,
                             a.OperateAddress,
                             a.OperateIP,
                             a.Title,
                             a.CreateTime,
                             AdminUserName = b.AdminUserName,
                             Name = c.Name,
                             c.AuthoryId
                         };
            if (!string.IsNullOrEmpty(username))
            {
                result=result.Where(m => m.AdminUserName == username);
            }
            if (authoryId > 0)
            {
                result=result.Where(m => m.AuthoryId == authoryId);
            }
            var finallyresult=result.Select(m => new { m.Action, m.AdminUserName, m.Content, m.Name, m.OperateAddress, m.OperateIP, m.Title,m.CreateTime });
            rowCount = finallyresult.Count();
            finallyresult = finallyresult.Skip(nowNumber).Take(pageSize);
            return finallyresult;
        }
    }
}
